using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Graphe.RobinsonPierpont;
using Graphe.Tools.TextualRangeGuesser;

namespace Graphe.WPF
{
    /// <summary>
    /// Interaction logic for TextualRangeGuesser.xaml
    /// </summary>

    public partial class TextualRangeGuesser : System.Windows.Window
    {

        public TextualRangeGuesser()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(TextualRangeGuesser_Loaded);
        }

        private BookCollection _books;

        void TextualRangeGuesser_Loaded(object sender, RoutedEventArgs e)
        {
            _books = Parser.Parse();

            Binding binding = new Binding();
            binding.Source = _books;
            _comboBoxBooks.DisplayMemberPath = "Name";
            _comboBoxBooks.SetBinding(ComboBox.ItemsSourceProperty, binding);
        }

        private void _buttonGuess_Click(object sender, RoutedEventArgs e)
        {
            if (_textBoxLines.Text == String.Empty)
            {
                _textBlockResult.Text = "How many lines?";
                return;
            }
            if (_textBoxCharactersPerLine.Text == String.Empty)
            {
                _textBlockResult.Text = "How many characters per line?";
                return;
            }

            int lineCount = Convert.ToInt32(_textBoxLines.Text);
            int charactersPerLine = Convert.ToInt32(_textBoxCharactersPerLine.Text);
            bool includePunctuation = _checkBoxIncludePunctuation.IsChecked.Value;
            bool includeSpaces = _checkBoxIncludeSpaces.IsChecked.Value;
            int columns = Convert.ToInt32((_comboBoxColumns.SelectedValue as ComboBoxItem).Content);

            Book book = _comboBoxBooks.SelectedItem as Book;
            Chapter chapter = _comboBoxChapters.SelectedItem as Chapter;
            Verse verse = _comboBoxVerses.SelectedItem as Verse;

            string guess = Guesser.Guess(columns, charactersPerLine, lineCount, book.Name, chapter.Number, verse.Number, includeSpaces, includePunctuation);
            _textBlockResult.Text = guess;
        }

        private void _comboBoxBooks_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Book book = _comboBoxBooks.SelectedItem as Book;

            Binding binding = new Binding();
            binding.Source = book.Chapters;
            _comboBoxChapters.DisplayMemberPath = "Number";
            _comboBoxChapters.SetBinding(ComboBox.ItemsSourceProperty, binding);
        }

        private void _comboBoxChapters_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Chapter chapter = _comboBoxChapters.SelectedItem as Chapter;

            Binding binding = new Binding();
            binding.Source = chapter.Verses;
            _comboBoxVerses.DisplayMemberPath = "Number";
            _comboBoxVerses.SetBinding(ComboBox.ItemsSourceProperty, binding);
        }

    }
}